if(!jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.envDef.envDtls.eMachines.EnvMachines")) {

    jsx3.lang.Package.definePackage("tibco.admin.enterpriseAssets.envDef.envDtls.eMachines", function (envMachinesPak) {
    });

    jsx3.lang.Class.defineClass("tibco.admin.enterpriseAssets.envDef.envDtls.eMachines.EnvMachines",
            jsx3.gui.Block,
            null,
            function (envMachines) {
                envMachines.prototype.mtxList = null;
                envMachines.prototype.environmentName = null
                envMachines.prototype.entityID = null;

                envMachines.prototype.subscribeAll = function() {
                    this.getDescendantOfName("btnEdit27F").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                    this.getDescendantOfName("mcAssociated27F").setValueTemplate(tibco.admin.Main.getServer().getDynamicProperty("@@Machines Enabled View Mode Value Template"));
                    this.mtxList = this.getDescendantOfName("mtxMachinesList27F");
                    this.getDescendantOfName("selFilterEnable27F").setValue(tibco.admin.Main.getServer().getDynamicProperty("@@Filter Option All"));
                    tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_ROW_SELECTED, this, this.setDetails);
                }

                envMachines.prototype.unsubscribeAll = function() {
                    tibco.admin.PubSub.unsubscribe(tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_ROW_SELECTED, this);
                }

                envMachines.prototype.clearAll = function() {
                    this.environmentName = null;
                    this.entityID = null;
                    tibco.admin.Main.getServer().getCache().clearById(this.mtxList.getXMLId());
                    this.mtxList.repaint();
                    var editButton = this.getDescendantOfName("btnEdit27F");
                    if(this.handler && editButton.getDisplay() == jsx3.gui.Block.DISPLAYNONE) {
                        this.handler.endEditMode();
                        editButton.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true).
                                getParent().getChild("btnCancel27F").setDisplay(jsx3.gui.Block.DISPLAYNONE, true).
                                getParent().getChild("btnAdd27F").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                        this.getDescendantOfName("mcAssociated27F").setValueTemplate(tibco.admin.Main.getServer().getDynamicProperty("@@Machines Enabled View Mode Value Template"));
//                        this.getDescendantOfName("mcAssociated27F").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true).
//                                getParent().getChild("mcAssociatedEdit27F").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                        this.mtxList.repaintData();
                    }
                    editButton.setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                    this.getDescendantOfName("selFilterEnable27F").setEnabled(jsx3.gui.Form.STATEDISABLED, true).setValue(tibco.admin.Main.getServer().getDynamicProperty("@@Filter Option All"));
                }

                envMachines.setToggle = function(jsxid, toggleMode) {
                    var record = tibco.admin.Main.getServer().getCache().getDocument("machineProductsEnv27H").selectSingleNode("//record[@jsxid='" + jsxid + "']");
                    if(record) {
                        record.setAttribute("isAssociated", toggleMode);
                    }
                }

                envMachines.prototype.setDetails = function(objEvent, useCached) {
                    if(this.handler && this.handler.isInEditMode()) {
                        return;
                    }
                    if(!objEvent) {
                        if(this.lastMsg && useCached) {
                            objEvent = this.lastMsg;
                        }
                    }
                    else {
                        this.lastMsg = objEvent;
                    }
                    if(objEvent && objEvent.environmentName) {
                        this.environmentName = objEvent.environmentName;
                        this.entityID = objEvent.environmentID;
                        this.callgetProductInventoryInEnterprise();
                    }
                    else {
                        this.clearAll();
                    }
                }

                envMachines.prototype.callgetProductInventoryInEnterprise = function() {
                    if(this.entityID && (!this.handler || !this.handler.isInEditMode())) {
                        var objService = tibco.admin.Main.loadService("tibco.admin",
                                "tibco/admin/enterpriseAssets/envDef/envDtls/eMachines/rules/getProductsAndInventoryInEnvRules.xml",
                                "getProductInventoryInEnterprise",
                                "Get products and inventory for environments");
                        //subscribe and call
                        objService.environmentName = this.environmentName;
                        objService.environmentID = this.entityID;
                        var me = this;
                        objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.ongetProductInventoryInEnterpriseSuccess);
                        objService.setEndpointURL(tibco.admin.util.ServiceHelper.getInventoryEndpointURL());
                        objService.doCall();
                    }
                };

                envMachines.prototype.ongetProductInventoryInEnterpriseSuccess = function(objEvent) {
                    if(this.mtxList.getXML().selectNodes("//record").getLength() > 0) {
                        this.getDescendantOfName("btnEdit27F").setEnabled(jsx3.gui.Form.STATEENABLED, true);
                        this.getDescendantOfName("selFilterEnable27F").setEnabled(jsx3.gui.Form.STATEENABLED, true)
                    }
                    this.mtxList.repaintData();
                    this.tempCacheDoc = this.mtxList.getXML().cloneDocument();
                };

                envMachines.prototype.callassociateEnvironmentToTIBCOInstallations = function() {
                    var objService = tibco.admin.Main.loadService("tibco.admin",
                            "tibco/admin/enterpriseAssets/envDef/envDtls/eMachines/rules/associateEnvToTibcoInstallationsRules.xml",
                            "associateEnvironmentToTIBCOInstallations",
                            "associate products and inventory for environments");
                    //subscribe and call
                    objService.environmentName = this.environmentName;
                    objService.environmentID = this.entityID;
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onassociateEnvironmentToTIBCOInstallationsSuccess);
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getManagementDaemonEndpointURL())
                    objService.doCall();
                };

                envMachines.prototype.onassociateEnvironmentToTIBCOInstallationsSuccess = function(objEvent) {
                    this.editSettings();
                    this.callgetProductInventoryInEnterprise();
                };

                envMachines.prototype.cancelEditSettings = function() {
                    this.editSettings();
                    this.callgetProductInventoryInEnterprise();
                }

                envMachines.prototype.editSettings = function() {
                    var button = this.getDescendantOfName("btnEdit27F");
                    if(button.getDisplay() == jsx3.gui.Block.DISPLAYBLOCK) {
                        this.handler.beginEditMode();
                        button.setDisplay(jsx3.gui.Block.DISPLAYNONE, true).
                                getParent().getChild("btnCancel27F").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true).
                                getParent().getChild("btnAdd27F").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                        this.getDescendantOfName("mcAssociated27F").setValueTemplate(tibco.admin.Main.getServer().getDynamicProperty("@@Machines Enabled Edit Mode Value Template"));
//                        this.getDescendantOfName("mcAssociated27F").setDisplay(jsx3.gui.Block.DISPLAYNONE, true).
//                                getParent().getChild("mcAssociatedEdit27F").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                    }
                    else {
                        this.handler.endEditMode();
                        button.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true).
                                getParent().getChild("btnCancel27F").setDisplay(jsx3.gui.Block.DISPLAYNONE, true).
                                getParent().getChild("btnAdd27F").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                        this.getDescendantOfName("mcAssociated27F").setValueTemplate(tibco.admin.Main.getServer().getDynamicProperty("@@Machines Enabled View Mode Value Template"));
//                        this.getDescendantOfName("mcAssociated27F").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true).
//                                getParent().getChild("mcAssociatedEdit27F").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);

                    }
                    this.mtxList.repaintData();
                }

                envMachines.prototype.filterResults = function() {
                    if(this.tempCacheDoc) {
                        var tempCacheDoc = this.tempCacheDoc;
                    }
                    var filterEnable = this.getDescendantOfName("selFilterEnable27F").getText();

                    if(filterEnable == tibco.admin.Main.getServer().getDynamicProperty("@@Filter Option All")) {
                        filterEnable = "All";
                    }
                    if(filterEnable == tibco.admin.Main.getServer().getDynamicProperty("@@Filter Option All Enabled")) {
                        filterEnable = "true";
                    }
                    if(filterEnable == tibco.admin.Main.getServer().getDynamicProperty("@@Filter Option All Not Enabled")) {
                        filterEnable = "false";
                    }

                    this.mtxList.setXMLTransformers(tibco.admin.Main.resolveURI("tibco.admin", "tibco/admin/enterpriseAssets/envDef/envDtls/eMachines/xsl/transform.xsl"));
                    this.mtxList.setXSLParam("enable", filterEnable);
                    this.mtxList.setSourceXML(tempCacheDoc);
                    this.mtxList.repaintData();
                }
            })
}